package com.neuedu.ud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.UUID;

/**
 * 项目：      springboot
 * 类名:       FileContontroll
 * 创建时间：  2024/3/28 14：10
 * 描述 :
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
@Controller
public class FileContontroll {


    @Value("spring.servlet.context-path")
    String contextPath ;

    public static final String  UPLOAD_DIR  = "D:\\upload\\boot\\";
    @RequestMapping("upload")
    String upload(MultipartFile pic) throws IOException {



        String oriName = pic.getOriginalFilename();//  原始文件名   a.png
        // 12198y72942794027427424279402831904
        String newFileName = UUID.randomUUID().toString().replace("-","") + oriName.substring(oriName.lastIndexOf("."));

        File dist = new File(UPLOAD_DIR, newFileName);
        pic.transferTo(dist);


        return "redirect:/list" ;
    }






    @RequestMapping("list")
    String list(Model model, HttpServletRequest request) throws IOException {

        File[] list = new File(UPLOAD_DIR).listFiles();

        model.addAttribute("fileList",list);

        // String contextpath = request.getContextPath();
        return "/list.jsp";
    }


    /**
     * http://localhost:8080/ud/d1?path=7cb20eb73ccc4177adb2c80ad2f907d6.png
     * @param path
     * @param response
     * @throws IOException
     */
    @GetMapping("/d1")
    public void download( String path , HttpServletResponse response) throws IOException {

        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.addHeader(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename="+path);

        InputStream is = new FileInputStream(new File(UPLOAD_DIR,path));
        OutputStream os = response.getOutputStream();
        int len = -1;
        byte[] bytes = new byte[1024];
        while (   (len = is.read(bytes)) != -1){
            os.write(bytes,0,len);
        }
        os.close();
        is.close();
    }


    /**
     * http://localhost:8080/ud/d2?path=7cb20eb73ccc4177adb2c80ad2f907d6.png
     * @param path
     * @param response
     * @return
     * @throws IOException
     */
    @GetMapping("/d2")
    public ResponseEntity download2( String path , HttpServletResponse response) throws IOException {

        ResponseEntity entity = ResponseEntity.ok().

                //设置下载 的文件名
                header(HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_OCTET_STREAM_VALUE).
                header(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename="+ URLEncoder.encode(path,"utf-8")).

                body(new FileSystemResource(new File(UPLOAD_DIR,path)));


        return entity;
    }




}
